package xyz.csmcool.imroom.mapper;

import org.apache.ibatis.annotations.*;
import xyz.csmcool.imroom.entity.Rooms;
import xyz.csmcool.imroom.entity.RoomsUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import xyz.csmcool.imroom.entity.vo.RoomUserVo;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author csmcool
 * @since 2022-08-08
 */
@Mapper
public interface RoomsUserMapper extends BaseMapper<RoomsUser> {


    /**
     * 房间用户查询
     * @param page
     * @param limit
     * @param roomId
     * @param createdBy
     * @return
     */
    @Select("SELECT `user` FROM `sjim_rooms_user` WHERE sjim_room =#{roomId} ORDER BY create_time " +
            "AND is_pass = 1 AND sjim_room IN(SELECT id FROM sjim_rooms WHERE created_by = #{userId}) " +
            "DESC LIMIT #{page},#{limit}")
    List<Integer> selectRoomUsers(@Param("page") Long page,@Param("limit") Long limit,
                                  @Param("roomId") Integer roomId,@Param("userId") Integer createdBy);

    /**
     * 房间用户查询
     * @param page
     * @param limit
     * @param createdBy
     * @return
     */
    @Select("SELECT `user` FROM `sjim_rooms_user` WHERE sjim_room =(SELECT id FROM sjim_rooms " +
            "WHERE created_by =#{userId} AND is_pass = 1  ORDER BY create_time DESC LIMIT 1) " +
            "ORDER BY create_time DESC LIMIT #{page},#{limit}")
    List<Integer> selectRoomUsersByTime(@Param("page") Long page,@Param("limit") Long limit,@Param("userId") Integer createdBy);

    /**
     * 查询所有用户的id
     * @param roomId
     * @param createdBy
     * @return
     */
    @Select("SELECT `user` FROM `sjim_rooms_user` WHERE sjim_room =#{roomId}" +
            " AND is_pass = 1 AND sjim_room IN(SELECT id FROM sjim_rooms WHERE created_by = #{userId}) ")
    List<Integer> selectAllUserId(@Param("roomId") Integer roomId,@Param("userId") Integer createdBy);

    /**
     * 房间用户查询
     * @param createdBy
     * @return
     */
    @Select("SELECT `user` FROM `sjim_rooms_user` " +
            "WHERE sjim_room =(SELECT id FROM sjim_rooms WHERE created_by =#{userId} ORDER BY create_time DESC LIMIT 1)" +
            " AND is_pass = 1 ")
    List<Integer> selectAllUserIdByTime(@Param("userId") Integer createdBy);

    /**
     * 查询用户总量
     * @param roomId
     * @return
     */
    @Select("SELECT count(*) FROM sjim_rooms_user WHERE sjim_room = #{id} AND is_pass = 1 ")
    Integer selectCount(@Param("id") Integer roomId);


    /**
     * 查询当前房间的用户
     * @param roomId
     * @param page
     * @param limit
     * @param id
     * @return
     */
    @Select("SELECT user FROM `sjim_rooms_user` " +
            "WHERE sjim_room = #{roomId} AND is_pass = 1 AND " +
            "#{id} = (SELECT user FROM `sjim_rooms_user` a WHERE sjim_room = #{roomId} AND is_pass = 1 AND `user` = #{id} ) " +
            "ORDER BY create_time LIMIT #{page},#{limit}")
    List<Integer> selectCurrentRoomUser(@Param("page") Long page,@Param("limit") Long limit,
                                        @Param("roomId") Integer roomId,@Param("id") Integer id);

    /**
     * 是否是房间用户*
     * @param roomId
     * @param id
     * @return
     */
    @Select("SELECT is_muted FROM `sjim_rooms_user` " +
            "WHERE sjim_room=#{roomId} AND `user` = #{id} AND is_pass = 1 LIMIT 1")
    Integer isRoomUser(@Param("roomId") Integer roomId,@Param("id") Integer id);

    /**
     * 禁言用户*
     * @param roomId
     * @param id
     * @param uid
     * @return
     */
    @Update("UPDATE sjim_rooms_user " +
            "SET is_muted = 1 " +
            "WHERE is_pass = 1 AND " +
            "sjim_room = (SELECT id FROM sjim_rooms WHERE id = #{roomId} AND created_by = #{uid} ) AND `user` = #{id}")
    Boolean mutedUser(@Param("roomId") Integer roomId,@Param("id") Integer id,
                      @Param("uid") Integer uid);

    /**
     * 解封用户*
     * @param roomId
     * @param id
     * @param uid
     * @return
     */
    @Update("UPDATE sjim_rooms_user " +
            "SET is_muted = 0 " +
            "WHERE is_pass = 1 AND " +
            "sjim_room = (SELECT id FROM sjim_rooms WHERE id = #{roomId} AND created_by = #{uid} ) AND `user` = #{id}")
    Boolean notMutedUser(@Param("roomId") Integer roomId,@Param("id") Integer id,
                      @Param("uid") Integer uid);

    /**
     * 查询房间
     * @param uid
     * @return
     */
    @Select("SELECT id FROM sjim_rooms WHERE created_by =#{userId} ORDER BY create_time DESC LIMIT 1")
    Long selectRoomIdByTime(@Param("userId") Integer uid);

    /**
     * 添加用户
     * @param uid
     * @param roomId
     * @param isPass
     * @return
     */
    @Insert("INSERT INTO sjim_rooms_user (`user`, sjim_room,is_pass) " +
            "VALUES (#{uid}, #{roomId}, #{pass}) ON DUPLICATE KEY UPDATE is_pass=#{pass};")
    Boolean addNewRoomUser(@Param("uid") Integer uid,@Param("roomId") Integer roomId,@Param("pass") Integer isPass);

    /**
     * 退出聊天室
     * @param uid
     * @param roomId
     * @return
     */
    @Delete("DELETE FROM sjim_rooms_user WHERE " +
            "`user` = #{uid} AND sjim_room = #{roomId} " +
            "AND #{roomId} != (SELECT id FROM `sjim_rooms` WHERE id = #{roomId} AND created_by = #{uid})")
    Boolean exitRoom(@Param("uid") Integer uid,@Param("roomId") Integer roomId);

    /**
     * 查看加入我的聊天室
     * @param page
     * @param limit
     * @param uid
     * @return
     */
    @Select("SELECT * FROM (" +
            "SELECT a.`name`,b.`user`,b.is_pass,a.id " +
            "FROM sjim_rooms a FORCE INDEX(index_create_by) LEFT JOIN  sjim_rooms_user b ON a.id = b.sjim_room " +
            "WHERE a.created_by = #{uid} ORDER BY b.create_time DESC " +
            ") c WHERE c.user <> #{uid} LIMIT #{page},#{limit}")
    List<RoomUserVo> selectAddMyRoom(@Param("page") Long page, @Param("limit") Long limit,
                                     @Param("uid") Integer uid);

    /**
     * 查看加入我的聊天室人数
     * @param uid
     * @return
     */
    @Select("SELECT COUNT(*) FROM (" +
            "SELECT a.`name`,b.`user`,b.is_pass,a.id FROM sjim_rooms a " +
            "FORCE INDEX(index_create_by) LEFT JOIN  sjim_rooms_user b ON a.id = b.sjim_room " +
            "WHERE a.created_by = #{uid} ORDER BY b.create_time DESC" +
            ") c WHERE c.user <> #{uid}")
    Long selectAddMyRoomCount(@Param("uid") Integer uid);

    /**
     * 查询用户状态
     * @param roomId
     * @param id
     * @return
     */
    @Select("SELECT is_pass FROM sjim_rooms_user WHERE `user` = #{id} AND sjim_room =#{roomId}")
    Integer selectJoinRoomUserStatus(@Param("roomId") Integer roomId,@Param("id") Integer id);

    /**
     * 查询用户状态
     * @param roomId
     * @param id
     * @param uid *
     * @return
     */
    @Select("SELECT is_pass FROM sjim_rooms_user a LEFT JOIN sjim_rooms b ON a.sjim_room = b.id " +
            "WHERE a.`user` = #{id} AND a.sjim_room = #{roomId} AND b.created_by = #{uid}")
    Integer selectRoomUserStatus(@Param("roomId") Integer roomId,@Param("id") Integer id,
                                 @Param("uid") Integer uid);
}
